﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GN.Pay.Utils;
using GN.Pay.Utils.Net.Http;
using GN.Pay.TradeChannel.Gyb.Response;
using GN.Pay.TradeChannel.ProtocolPay;

namespace GN.Pay.TradeChannel.Gyb.Request
{
    /// <summary>
    /// 协议支付请求
    /// </summary>
    [Serializable]
    class ProtocolBindRequest : ProtocolBaseRequest
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="apply"></param>
        /// <param name="config"></param>
        public ProtocolBindRequest(IChannel channel,
           IProtocolBindApply apply,
           GybConfigure config):base(channel, apply, config)
        {
           
        }

        /// <summary>
        /// 
        /// </summary>
        public override string TranAbbr
        {
            get
            {
                return TradeCommand.XYQY;
            }
        }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="channel">通道</param>
        /// <returns></returns>
        public Response.ProtocolBindResponse Build(ITradeChannel channel)
        {
            var plain = BuildPlain();
            var dic = new SignDictionary();
            dic["transName"] = this.TranAbbr;
            dic["Plain"] = plain;
            dic["Signature"] = SecurityUtils.Sign(Config.ConfigureInfo, plain);

            string requestContent = BuildXML(dic, Config);

            HttpClient client = new HttpClient();
            client.Encoding = Config.Encoding;
            client.ContentType = "text/xml";
            var response = client.PostString(new Uri(Config.GatewayUrl), requestContent);

            return new ProtocolBindResponse(response, channel, Config);
        }
    }
}
